home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / ARC / ARCPACK.MAC < prev    next >
Text File  |  1986-02-03  |  10KB  |  248 lines

  1. /*  ARC - Archive utility - ARCPACK
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =3.37), created on $tag(
  6. TED_DATE DB =02/03/86) at $tag(
  7. TED_TIME DB =22:58:01))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the routines used to compress a file
  17.          when placing it in an archive.
  18.  
  19.     Language:
  20.          Computer Innovations Optimizing C86
  21. */
  22. #include <stdio.h>
  23. #include "arc.h"
  24.  
  25. /* stuff for non-repeat packing */
  26.  
  27. #define DLE 0x90                       /* repeat sequence marker */
  28.  
  29. static unsigned char state;            /* current packing state */
  30.  
  31. /* non-repeat packing states */
  32.  
  33. #define NOHIST  0                      /* don't consider previous input*/
  34. #define SENTCHAR 1                     /* lastchar set, no lookahead yet */
  35. #define SENDNEWC 2                     /* run over, send new char next */
  36. #define SENDCNT 3                      /* newchar set, send count next */
  37.  
  38. /* packing results */
  39.  
  40. static long stdlen;                    /* length for standard packing */
  41. static int crcval;                     /* CRC check value */
  42.  
  43. pack(f,t,hdr)                          /* pack file into an archive */
  44. FILE *f, *t;                           /* source, destination */
  45. struct heads *hdr;                     /* pointer to header data */
  46. {
  47.     int c;                             /* one character of stream */
  48.     long ncrlen;                       /* length after packing */
  49.     long huflen;                       /* length after squeezing */
  50.     long lzwlen;                       /* length after crunching */
  51.     long pred_sq(), file_sq();         /* stuff for squeezing */
  52.     long pred_cm();                    /* dynamic crunching cleanup */
  53.     char tnam[$strlen];                /* temporary name buffer */
  54.     char *makefnam();                  /* filename fixer upper */
  55.     FILE *crn = NULL;                  /* temporary crunch file */
  56.  
  57.     /* first pass - see which method is best */
  58.  
  59.     if(!nocomp)                        /* if storage kludge not active */
  60.     {    if(note)
  61.               printf(" analyzing, ");
  62.  
  63.          if(arctemp)                   /* use temp area if specified */
  64.               sprintf(tnam,"%s$ARCTEMP.CRN",arctemp);
  65.          else makefnam("$ARCTEMP.CRN",arcname,tnam);
  66.          crn = fopen(tnam,"wrb");
  67.  
  68.          state = NOHIST;               /* initialize ncr packing */
  69.          stdlen =  ncrlen = 0;         /* reset size counters */
  70.          crcval = 0;                   /* initialize CRC check value */
  71.          setcode();                    /* initialize encryption */
  72.  
  73.          init_cm(f,crn);               /* initialize for crunching */
  74.          init_sq();                    /* initialize for squeeze scan */
  75.  
  76.          while((c=getc_ncr(f))!=EOF)   /* for each byte of file */
  77.          {    ncrlen++;                /* one more packed byte */
  78.               scan_sq(c);              /* see what squeezing can do */
  79.               putc_cm(c,crn);          /* see what crunching can do */
  80.          }
  81.          huflen = pred_sq();           /* finish up after squeezing */
  82.          lzwlen = pred_cm(crn);        /* finish up after crunching */
  83.     }
  84.     else                               /* else kludge the method */
  85.     {    stdlen = 0;                   /* make standard look best */
  86.          ncrlen = huflen = lzwlen = 1;
  87.     }
  88.  
  89.     /* standard set-ups common to all methods */
  90.  
  91.     fseek(f,0L,0);                     /* rewind input */
  92.     hdr->crc = crcval;                 /* note CRC check value */
  93.     hdr->length = stdlen;              /* set actual file length */
  94.     state = NOHIST;                    /* reinitialize ncr packing */
  95.     setcode();                         /* reinitialize encryption */
  96.  
  97.     /* choose and use the shortest method */
  98.  
  99.     if(stdlen<=ncrlen && stdlen<=huflen && stdlen<=lzwlen)
  100.     {    if(kludge)                    /*DEBUG*/
  101.               printf("(%ld) ",lzwlen-stdlen);
  102.          if(note)
  103.               printf("storing, ");     /* store without compression */
  104.          hdrver = 2;                   /* note packing method */
  105.          stdlen = crcval = 0;          /* recalc these for kludge */
  106.          while((c=getch(f))!=EOF)      /* store it straight */
  107.               putc_pak(c,t);
  108.          hdr->crc = crcval;
  109.          hdr->length = hdr->size = stdlen;
  110.     }
  111.  
  112.     else if(ncrlen<huflen && ncrlen<lzwlen)
  113.     {    if(kludge)                    /*DEBUG*/
  114.               printf("(%ld) ",lzwlen-ncrlen);
  115.          if(note)
  116.               printf("packing, ");     /* pack with repeat suppression */
  117.          hdrver = 3;                   /* note packing method */
  118.          hdr->size = ncrlen;           /* set data length */
  119.          while((c=getc_ncr(f))!=EOF)
  120.               putc_pak(c,t);
  121.     }
  122.  
  123.     else if(huflen<lzwlen)
  124.     {    if(kludge)                    /*DEBUG*/
  125.               printf("(%ld) ",lzwlen-huflen);
  126.          if(note)
  127.               printf("squeezing, ");
  128.          hdrver = 4;                   /* note packing method */
  129.          hdr->size = file_sq(f,t);     /* note final size */
  130.     }
  131.  
  132.     else
  133.     {    if(kludge)                    /*DEBUG*/
  134.               printf("(%ld) ",huflen-lzwlen);
  135.          if(note)
  136.               printf("crunching, ");
  137.          hdrver = 8;
  138.          hdr->size = lzwlen;           /* size should not change */
  139.          if(crn)                       /* if temp was created */
  140.          {    fseek(crn,0L,0);         /* then copy over crunched temp */
  141.               while((c=fgetc(crn))!=EOF)
  142.                    putc_tst(c,t);
  143.          }
  144.          else                          /* else re-crunch */
  145.          {    init_cm(f,t);
  146.               while((c=getc_ncr(f))!=EOF)
  147.                    putc_cm(c,t);
  148.               pred_cm(t);              /* finish up after crunching */
  149.          }
  150.     }
  151.  
  152.     /* standard cleanups common to all methods */
  153.  
  154.     if(crn)                            /* get rid of crunch temporary */
  155.     {    fclose(crn);
  156.          if(unlink(tnam) && warn)
  157.          {    printf("Cannot delete temporary file %s\n",tnam);
  158.               nerrs++;
  159.          }
  160.     }
  161.     if(note)
  162.          printf("done.\n");
  163. }
  164.  
  165. /*  Non-repeat compression - text is passed through normally, except that
  166.     a run of more than two is encoded as:
  167.  
  168.          <char> <DLE> <count>
  169.  
  170.     Special case: a count of zero indicates that the DLE is really a DLE,
  171.     not a repeat marker.
  172. */
  173.  
  174. int getc_ncr(f)                        /* get bytes with collapsed runs */
  175. FILE *f;                               /* file to get from */
  176. {
  177.     static int lastc;                  /* value returned on last call */
  178.     static int repcnt;                 /* repetition counter */
  179.     static int c;                      /* latest value seen */
  180.  
  181.     switch(state)                      /* depends on our state */
  182.     {
  183.     case NOHIST:                       /* no relevant history */
  184.          state = SENTCHAR;
  185.          return lastc = getch(f);      /* remember the value next time */
  186.  
  187.     case SENTCHAR:                     /* char was sent. look ahead */
  188.          switch(lastc)                 /* action depends on char */
  189.          {
  190.          case DLE:                     /* if we sent a real DLE */
  191.               state = NOHIST;          /* then start over again */
  192.               return 0;                /* but note that the DLE was real */
  193.  
  194.          case EOF:                     /* EOF is always a special case */
  195.               return EOF;
  196.  
  197.          default:                      /* else test for a repeat */
  198.               for(repcnt=1; (c=getch(f))==lastc && repcnt<255; repcnt++)
  199.                    ;                   /* find end of run */
  200.  
  201.               switch(repcnt)           /* action depends on run size */
  202.               {
  203.               case 1:                  /* not a repeat */
  204.                    return lastc = c;   /* but remember value next time */
  205.  
  206.               case 2:                  /* a repeat, but too short */
  207.                    state = SENDNEWC;   /* send the second one next time */
  208.                    return lastc;
  209.  
  210.               default:                 /* a run - compress it */
  211.                    state = SENDCNT;    /* send repeat count next time */
  212.                    return DLE;         /* send repeat marker this time */
  213.               }
  214.          }
  215.  
  216.     case SENDNEWC:                     /* send second char of short run */
  217.          state = SENTCHAR;
  218.          return lastc = c;
  219.  
  220.     case SENDCNT:                      /* sent DLE, now send count */
  221.          state = SENDNEWC;
  222.          return repcnt;
  223.  
  224.     default:
  225.          abort("Bug - bad ncr state\n");
  226.     }
  227. }
  228.  
  229. static int getch(f)                    /* special get char for packing */
  230. FILE *f;                               /* file to get from */
  231. {
  232.     int c;                             /* a char from the file */
  233.  
  234.     if((c=fgetc(f))!=EOF)              /* if not the end of file */
  235.     {    crcval = addcrc(crcval,c);    /* then update CRC check value */
  236.          stdlen++;                     /* and bump length counter */
  237.     }
  238.  
  239.     return c;
  240. }
  241.  
  242. putc_pak(c,f)                          /* put a packed byte into archive */
  243. char c;                                /* byte to put */
  244. FILE *f;                               /* archive to put it in */
  245. {
  246.     putc_tst(code(c),f);               /* put encoded byte, with checks */
  247. }
  248.